<template>
  <div v-if="props.readonly==1">{{format}}</div>
  <el-select v-model="name" placeholder="请选择" filterable clearable v-else>
    <el-option v-for="item in options" :key="item.id" :label="item.supplierName" :value="item[valueField]" />
  </el-select>
</template>

<script setup lang="ts">
import { computed, onMounted } from "vue";
const { proxy } = getCurrentInstance();
let props = defineProps(["modelValue", "val", "readonly", "supplier"]);
const emit = defineEmits(["update:modelValue"]);

let list = ref([]);

const name = computed({
  get() {
    return props.modelValue;
  },
  set(value: Number) {
    emit("update:modelValue", value);
  },
});
let valueField = computed(() => {
  return props.val ? props.val : "id";
});
let options = computed(() => {
  if (props.supplier) {
    let arr = [];
    list.value.forEach((element) => {
      if (props.supplier.includes(element.id)) arr.push(element);
    });
    return arr;
  } else {
    return list.value;
  }
});

onMounted(() => {
  getInfo();
});
let format = computed(() => {
  let index = proxy.$utils.findIndexByField(
    options.value,
    valueField.value,
    props.modelValue
  );
  if (index != -1) return options.value[index].supplierName;
});

let getInfo = async () => {
  let { data } = await proxy.$apis.supplierQuery();
  list.value = [...data];
};
</script>

<style lang="scss" scoped>
</style>
